package com.lagou.edu.service.impl;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ResumeServiceImpl implements ResumeService {

    @Autowired
    private ResumeDao resumeDao;

    @Override
    public List<Resume> getResumes() {
        return resumeDao.findAll();
    }

    @Override
    public Resume getResumeById(Long id) {

        Resume resume = new Resume();

        Optional<Resume> resumeOptional = resumeDao.findById(id);
        if (resumeOptional.isPresent()) {
            resume = resumeOptional.get();
        }

        return resume;
    }

    @Override
    public void removeResume(Long id) {
        resumeDao.deleteById(id);
    }

    @Override
    public void addResume(String address, String name, String phone) {

        Resume resume = new Resume();
        resume.setAddress(address);
        resume.setName(name);
        resume.setPhone(phone);

        resumeDao.save(resume);
    }

    @Override
    public void updateResume(Long id, String address, String name, String phone) {

        Optional<Resume> resumeOptional = resumeDao.findById(id);
        if (resumeOptional.isPresent()) {
            Resume resume = resumeOptional.get();

            resume.setAddress(address);
            resume.setName(name);
            resume.setPhone(phone);

            resumeDao.save(resume);
        }
    }
}
